package net.mfjassociates.financial.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import net.mfjassociates.financial.client.managed.request.ApplicationRequestFactory;
import net.mfjassociates.financial.client.managed.request.DocumentProxy;
import net.mfjassociates.financial.client.managed.request.DocumentRequest;
import net.mfjassociates.financial.client.managed.ui.DocumentDetailsView;
import net.mfjassociates.financial.client.managed.ui.DocumentEditView;
import net.mfjassociates.financial.client.managed.ui.DocumentListView;
import net.mfjassociates.financial.client.managed.ui.DocumentMobileDetailsView;
import net.mfjassociates.financial.client.managed.ui.DocumentMobileEditView;
import net.mfjassociates.financial.client.scaffold.ScaffoldApp;
import net.mfjassociates.financial.client.scaffold.place.CreateAndEditProxy;
import net.mfjassociates.financial.client.scaffold.place.FindAndEditProxy;
import net.mfjassociates.financial.client.scaffold.place.ProxyPlace;
import net.mfjassociates.financial.shared.domain.Category;

public class DocumentActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public DocumentActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new DocumentDetailsActivity((EntityProxyId<DocumentProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? DocumentMobileDetailsView.instance() : DocumentDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<net.mfjassociates.financial.client.managed.request.DocumentProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<DocumentProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        DocumentEditView.instance().setCreating(true);
        final DocumentRequest request = requests.documentRequest();
        Activity activity = new CreateAndEditProxy<DocumentProxy>(DocumentProxy.class, request, ScaffoldApp.isMobile() ? DocumentMobileEditView.instance() : DocumentEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(DocumentProxy proxy) {
            	proxy.setCreatedBy(DocumentEditView.getCurrentUser());
                request.persist().using(proxy);
                return request;
            }
        };
        return new DocumentEditActivityWrapper(requests, ScaffoldApp.isMobile() ? DocumentMobileEditView.instance() : DocumentEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        DocumentEditView.instance().setCreating(false);
        EntityProxyId<DocumentProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<DocumentProxy>(proxyId, requests, ScaffoldApp.isMobile() ? DocumentMobileEditView.instance() : DocumentEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(DocumentProxy proxy) {
                DocumentRequest request = requests.documentRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new DocumentEditActivityWrapper(requests, ScaffoldApp.isMobile() ? DocumentMobileEditView.instance() : DocumentEditView.instance(), activity, proxyId);
    }
}
